<?php
require_once("main.php");

$host = "localhost";
$user = "achatell";
$bdd = "achatell";
$passwd  = "**123456";

mysql_connect($host, $user,$passwd) or die("server connexion error");
mysql_select_db($bdd) or die("database connexion error");

if(isTokenExist()){
	mysql_query("BEGIN");

	$delete_query = "DELETE FROM ExtensionData where userId='".getToken()
	."' and extensionId='".getExtension()."'";
	mysql_query($delete_query);

	$data = getData();
	foreach ($data as &$element) {
		$insert_query = "INSERT INTO ExtensionData (userId, extensionId, fieldName, fieldValue) VALUES('"
		. getToken() . "','" . getExtension() . "','"
		. $element['name'] . "','" . $element['value'] . "');";
		$db_error = !mysql_query($insert_query);
		if($db_error){
			break;
		}
	}

	if($db_error){
		mysql_query("ROLLBACK");
		$result['data'] = "";
		$result['error'] = "Database error";
	} else {
		mysql_query("COMMIT");
		$result['data'] = "";
	}
} else {
	$result['data'] = "";
	$result['error'] = "Authentication error";
}

echo json_encode($result);
mysql_close();
?>